package com.xiaohudie.SGGio;

import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @Author xiao hu die
 * @Date 2022/3/24 15:03
 * @Version 1.0
 */
public class BufferTest {
 public static void main(String[] args) {
//test01();
// test02();
 //  test03();
 test04();
 }
 public static void test01(){
  File file = new File("D:\\java图片代码\\carbon (1).png");
  File file1 = new File("D:\\java图片代码\\66.png");
  FileInputStream fileInputStream =  null;
  FileOutputStream fileOutputStream =  null;
  BufferedInputStream bufferedInputStream =null;
  BufferedOutputStream bufferedOutputStream =null;
  try {
  fileInputStream =new FileInputStream(file);
  fileOutputStream =new FileOutputStream(file1);
  bufferedInputStream =new BufferedInputStream(fileInputStream);
  bufferedOutputStream =new BufferedOutputStream(fileOutputStream);
  byte[] bytes =new byte[1024];
  int len;
  while ((len =bufferedInputStream.read(bytes))!=-1){
   bufferedOutputStream.write(bytes,0,len);
  }
 }catch (IOException e){
   e.printStackTrace();
  }finally {
   if (bufferedInputStream!=null){
    try {
     bufferedInputStream.close();
    } catch (IOException e) {
     e.printStackTrace();
    }
   }
   if (bufferedOutputStream!=null){
    try {
     bufferedOutputStream.close();
    } catch (IOException e) {
     e.printStackTrace();
    }
   }
  }
 }
 public static void test02(){
  BufferedReader bufferedReader =null;
  BufferedWriter bufferedWriter =null;
  try {
  bufferedReader =new BufferedReader(new FileReader(new File("D:\\java图片代码\\22.txt")));
  bufferedWriter = new BufferedWriter(new FileWriter(new File("D:\\java图片代码\\77.txt")));
  /*char[] chars =new char[5];
  int len;
  while ((len =bufferedReader.read(chars))!=-1){
   bufferedWriter.write(chars,0,len);
  }*/
  String string ;
  while ((string =bufferedReader.readLine())!=null){
   bufferedWriter.newLine();
   bufferedWriter.write(string);
  }

  }catch (IOException e){
   e.printStackTrace();
  }finally {
   if (bufferedReader!=null){
    try {
     bufferedReader.close();
    } catch (IOException e) {
     e.printStackTrace();
    }
   }
   if (bufferedWriter!=null){
    try {
     bufferedWriter.close();
    } catch (IOException e) {
     e.printStackTrace();
    }
   }
  }
 }
 public static void  test03(){
  BufferedInputStream bufferedInputStream =null;
  BufferedOutputStream bufferedOutputStream =null;
  try {
   bufferedInputStream = new BufferedInputStream(new FileInputStream(new File("D:\\java图片代码\\99.png")));
   bufferedOutputStream =new BufferedOutputStream(new FileOutputStream("D:\\java图片代码\\1010.png"));
   byte[] bytes =new byte[1024];
   int len;
   while ((len = bufferedInputStream.read(bytes))!=-1){
        /**对字节数组进行修改*/
   /* for (byte aByte : bytes) {
     aByte =(byte) (aByte ^5);
    }*/
    for (int i = 0; i < bytes.length ; i++) {
     bytes[i] = (byte) (bytes[i] ^5);
    }
        bufferedOutputStream.write(bytes,0,len);
   }
  }catch (IOException e){
   e.printStackTrace();
  }finally {
   if (bufferedInputStream!=null){
    try {
     bufferedInputStream.close();
    } catch (IOException e) {
     e.printStackTrace();
    }
   }
   if (bufferedOutputStream!=null){
    try {
     bufferedOutputStream.close();
    } catch (IOException e) {
     e.printStackTrace();
    }
   }
  }
 }
 public static void test04(){
  BufferedReader bufferedInputStream =null;
  BufferedWriter bufferedOutputStream =null;
  HashMap<Character,Integer> hashMap =new HashMap<Character, Integer>();
  try {
  bufferedInputStream = new BufferedReader(new FileReader("D:\\java图片代码\\22.txt"));
  bufferedOutputStream = new BufferedWriter(new FileWriter("D:\\java图片代码\\1111.txt"));
  char[] chars =new char[5];
  int len;
  while ((len =bufferedInputStream.read(chars))!=-1){
   /**将字节转换为字符*/
   char c = (char) len;
   /**判断map里面是否有这个值*/
   if (hashMap.get(c)==null){
    hashMap.put(c,1);
   }else {
    hashMap.put(c, hashMap.get(c)+1);
   }
   /**把map写入到磁盘中*/
/*   Set<Character> characters = hashMap.keySet();
   for (Character character : characters) {
    bufferedOutputStream.write(String.valueOf(character) +"=" +hashMap.get(character));
   }*/
   Set<Map.Entry<Character, Integer>> entries = hashMap.entrySet();
   for (Map.Entry<Character, Integer> entry : entries) {
    switch (entry.getKey()){
     case ' ':
      bufferedOutputStream.write("空格"+"="+entry.getValue());
     case '\t':
      bufferedOutputStream.write("tab健"+"="+entry.getValue());
     case '\r':
      bufferedOutputStream.write("回车"+"=" +entry.getValue());
     default:
      bufferedOutputStream.write(entry.getKey()+"="+entry.getValue());
      break;
    }
    bufferedOutputStream.newLine();
   }
  }
  }catch (IOException e){
   e.printStackTrace();
  }finally {
   if (bufferedInputStream!=null){
    try {
     bufferedInputStream.close();
    } catch (IOException e) {
     e.printStackTrace();
    }
   }
   if (bufferedOutputStream!=null){
    try {
     bufferedOutputStream.close();
    } catch (IOException e) {
     e.printStackTrace();
    }
   }
  }
 }
}
